<?php
header("Content-type:text/html; charset=utf-8");
require_once ('include/conn.php');
require_once ('system.class.php');
function arcpagelist($args, $content, &$smarty){

	/* 定义全局变量pageinfo,用于生成静态栏目页时使用
	 * 由于limit是在模板中指定的,而在生成栏目静态页时
	* 需要根据limit判断需要生成静态页的数量,故从此处
	* 获取。
	* */
	$pageinfo=array();
	$channel_id=$args['channel_id'];
	$limit=$args['limit'];
	if($limit<0){
		$limit=20;
	}
	$pageinfo['limit']=$limit;
	$offset=$args['offset'];
	if($offset<0){
		$offset=0;
	}
	/*获取文章所在栏目信息*/
	$channel_information=getArticleChannelInformations($channel_id);

	$pageinfo['channel_name']=$channel_information['channel_name'];
	/*递归获取最近子栏*/
	getFirstBottomChild($channel_id);

	/*获取最近子栏目信息*/
	$article_channel_information=getArticleChannelInformations($channel_id);
	$info['channel_name']=$article_channel_information['channel_name'];
	$sql_get_articles_number="select * from article_normal where article_main_channel=$channel_id and article_status=1";

	/*获取文章个数*/
	$result_get_articles_number=mysql_query($sql_get_articles_number) or die(mysql_error());
	$numrows=mysql_num_rows($result_get_articles_number);
	if($offset>$numrows){
		$offset=($numrows%$limit==0)?($numrows-$limit):($numrows-($numrows%$limit));
	}
	if($offset%$limit!=0){
		$offset=$offset-$offset%$limit;
	}
	$pageinfo['article_num']=$numrows;
		$info['url']=CIE."/channel.php?channel_id=$channel_id";
	/*更新置顶的文章,将过期的文件取消置顶设置*/
	$time=time();
	$sql_update_is_top_articles="update article_normal set article_is_top=0 where article_is_top=1 and article_top_time<$time;";
	$result_update_is_top_articles=mysql_query($sql_update_is_top_articles);

	/*获取文章信息*/
	$sql_get_articles_information="select article_id,article_title,article_is_top,article_publish_time,article_modify_time,article_is_extends_url,article_extends_url,article_click from article_normal
	where article_status=1 and article_main_channel=$channel_id order by article_is_top desc,article_modify_time desc limit $offset,$limit";
	$result_get_articles_information=mysql_query($sql_get_articles_information) or die(mysql_error());
	/*显示查询结果*/
	while ($data=mysql_fetch_row($result_get_articles_information)) {
		$article_id=$data[0];
		$arr[$article_id]['id']=$data[0];
		$arr[$article_id]['title']=$data[1];
		if($data[5]==1){
			$arr[$article_id]['url']=$data[6];
		}else{
			$arr[$article_id]['url']=CIE."/article.php?article_id=$data[0]&channel_id=$channel_id";
		}
		$arr[$article_id]['publish_time']=date('Y-m-d',$data[3]);
		$arr[$article_id]['click_time']=$data[7];
		$arr[$article_id]['top']=($data[2]==1)?'true':'false';
		$now_time=time();
		$arr[$article_id]['new']=($now_time-$data[3]<=60*60*24)?'true':'false';		
	}
	/*文章数*/
	$info['article_num']=$numrows;
	/*首页*/
	$link=CIE."/channel.php?offset=0&channel_id=$channel_id";
	$info['article_home']="<span class='page'><a href='$link'>首页</a></span>";
	/*上一页*/
	$info['pre_page']="";
	if ($offset >0) {
		$prevoffset=$offset-$limit;
		if($prevoffset>=0){
			$link=CIE."/channel.php?offset=$prevoffset&channel_id=$channel_id";
			$info['pre_page']="<span class='page'><a href='$link'>上一页</a></span>";
		}
	}
	/*计算页面数*/
	$pages=intval($numrows/$limit);

	/*如果有余数，加一*/
	if ($numrows%$limit) {
		$pages++;
	}
	/*显示页数*/
	$info['pages']=null;
	$current_num=$offset/$limit+1;
	$i=($current_num>3)?($current_num-2):1;
	$limit_num=$i+5;
	while($i<=$pages&&$i<$limit_num) {
		$newoffset=$limit*($i-1);
		$page_num=$i;
		if($i==$current_num){
			$page_num="<span class='current_page'>$i</span>";
		}
		$link=CIE."/channel.php?offset=$newoffset&channel_id=$channel_id";
		$info['pages'][$i]="<span class='page'><a href='$link'>$page_num</a></span>";
		$i++;
	}

	/*下一页*/
	$info['next_page']="";
	if (!(($offset/$limit)==$pages) && $pages!=1) {
		// not last page so give NEXT link
		$newoffset=$offset+$limit;
		if($newoffset<$numrows){
			$link=CIE."/channel.php?offset=$newoffset&channel_id=$channel_id";
			$info['next_page']="<span class='page'><a href='$link'>下一页</a></span>";
		}
	}
	/*尾页*/
	$newoffset=($numrows%$limit)?($numrows-$numrows%$limit):($numrows-$numrows%$limit-$limit);
	if($newoffset<0){
		$newoffset=0;
	}
	$link=CIE."/channel.php?offset=$newoffset&channel_id=$channel_id";
	$info['last_page']="<span class='page'><a href='$link'>尾页</a></span>";
	if(empty($info)){
		$infor=null;
	}
	if(empty($arr)){
		$arr=null;
	}
	$GLOBALS['pageinfo']=$pageinfo;
	$smarty->assign("list",$arr);
	$smarty->assign("info",$info);
	return $content;
}


function getArticleChannelInformations($channel_id){
	$sql_get_article_channel_information="select channel_name,channel_visit_path,is_bottom from channel where channel_id=$channel_id";
	$result_get_article_channel_information=mysql_query($sql_get_article_channel_information) or die(mysql_error());
	$row_get_article_channel_information=mysql_fetch_row($result_get_article_channel_information);
	return array("channel_name"=>$row_get_article_channel_information[0],"channel_visit_path"=>$row_get_article_channel_information[1],"is_bottom"=>$row_get_article_channel_information[2]);
}

function getFirstBottomChild(&$channel_id){
	$channel_infor=getArticleChannelInformations($channel_id);
	if($channel_infor['is_bottom']!=0){
		return;
	}
	else{
		$sql_get_child_channel="select channel_id from channel where pre_id=$channel_id order by channel_id desc";
		$result_get_child_channel=mysql_query($sql_get_child_channel);
		while($row_get_child_channel=mysql_fetch_row($result_get_child_channel)){
			$channel_id=$row_get_child_channel[0];
			getFirstBottomChild($channel_id);
		}
	}
}